
package com.tools.cleanmaster.model.item;

import android.os.Build;
import android.text.TextUtils;

import com.tools.cleanmaster.model.TrashType;
import com.tools.cleanmaster.utils.FileHelper;
import com.tools.cleanmaster.utils.OptimizerFile;
import com.tools.cleanmaster.utils.TrashUtils;

import java.io.File;
import java.io.Serializable;

/**
 * 垃圾项基类
 */
public class TrashItem implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 路径id
     */
    public long id;

    /**
     * 垃圾类型
     */
    public TrashType trashType;

    /**
     * 应用包名，此字段仅对APK类型的垃圾有效：</br> {@link TrashType#APP_CACHE},</br>
     * {@link TrashType#APK_FILE},</br> {@link TrashType#UNINSTALLED_APP}
     */
    public String pkgName;

    /**
     * 应用名称，此字段仅对APK类型的垃圾有效：</br> {@link TrashType#APP_CACHE},</br>
     * {@link TrashType#APK_FILE},</br> {@link TrashType#UNINSTALLED_APP}
     */
    public String appName;

    /**
     * 文件路径（或目录），此字段仅对文件类型的垃圾有效：</br> {@link TrashType#LOG_FILE},</br>
     * {@link TrashType#TEMP_FILE},</br> {@link TrashType#EMPTY_FOLDER},</br>
     * {@link TrashType#APK_FILE},</br> {@link TrashType#LARGE_FILE},</br>
     * {@link TrashType#THUMBNAIL},</br> {@link TrashType#UNINSTALLED_APP}
     */
    public String filePath;

    /**
     * 垃圾大小，具体含义如下：</br> 对于{@link TrashType#APP_CACHE}类型，该大小指缓存大小；</br> 对于
     * {@link TrashType#EMPTY_FOLDER}类型，此字段无效；</br> 对于其它类型，均为所有垃圾文件总大小
     */
    public long size;

    public int fileCount;

    /**
     * 垃圾清理状态标记
     */
    protected boolean cleanedFlag;

    public boolean isSelected = true;
    public boolean isSelectedDefault = true;
    public boolean isDir;

    /**
     * 获取该垃圾的描述信息，可能是应用名称或者文件路径，用于扫描过程中动态显示扫描状态。
     *
     * @return 返回该描述信息
     */
    public String getTrashDesc() {
        if (trashType == TrashType.APP_CACHE) {
            return appName;
        } else {
            return filePath;
        }
    }

    /**
     * 清理该项垃圾 对于非TrashGroup的TrashItem需要在外层调用TrashesManager.getTrashes().
     * cleanTrashItems()来将清除信息注入DB
     */
    public void clean() {
        if (TrashUtils.isUseJniFunction() && Build.VERSION.SDK_INT != 19) {
            TrashUtils.deleteFileJni(new File(filePath), null);
        } else {
            try {
                FileHelper.deleteFile(new OptimizerFile(filePath));
            } catch (OutOfMemoryError e) {
            }
        }
        cleanedFlag = true;
    }

    /**
     * 清理该项垃圾
     * 对于非TrashGroup的TrashItem需要在外层调用TrashesManager.getTrashes().cleanTrashItems()来将清除信息注入DB
     * exclude  不需要删除的后缀
     */
    public void clean(String suffixExclude) {
        if (TextUtils.isEmpty(suffixExclude)) {
            clean();
        } else {
            if (TrashUtils.isUseJniFunction() && Build.VERSION.SDK_INT != 19) {
                TrashUtils.deleteFileJni(new File(filePath), suffixExclude);
            } else {
                TrashUtils.deleteFile(new OptimizerFile(filePath));
            }
            cleanedFlag = true;
        }
    }

    /**
     * 该项垃圾是否已被清理
     */
    public boolean isCleaned() {
        return cleanedFlag;
    }
}
